﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Freshome.p.Controllers
{
    public class SeekController : Controller
    {
        [OutputCache(Duration = 1)]
        public ActionResult Index(string kword, short mjd = default(short), short mid = default(short))
        {
            Models.Seekeeps sks = new Models.Seekeeps();
            if (!string.IsNullOrEmpty(kword))
            {
                RecipeBoy rb = new RecipeBoy();
                MajorawBoy mjb = new MajorawBoy();
                MinorawBoy mib = new MinorawBoy();
                var rps = rb.SeekRecipes(kword);
                if (mjd > default(short))
                {
                    sks.Rps = rps.Where(r => r.Majors.Any(m => m.Jmx == mjd));
                    var maj = mjb.GetEntity(m => m.Mid == mjd);
                    if (maj != null)
                    {
                        sks.Majoraw_nm = maj.Name;
                        sks.Majoraw_nt = maj.Notes;
                    }
                }
                else if (mid > default(short))
                {
                    sks.Rps = rps.Where(r => r.Minors.Any(m => m.Imx == mid));
                    var mai = mib.GetEntity(m => m.Mid == mid);
                    if (mai != null)
                    {
                        sks.Minoraw_nm = mai.Name;
                        sks.Minoraw_nt = mai.Notes;
                    }
                }
                else sks.Rps = rps;
                sks.Mjs = mjb.SeekMajoraws(kword);
                sks.Mis = mib.SeekMinoraws(kword);
                sks.Neword = kword;
                sks.Majoraw_id = mjd;
                sks.Minoraw_id = mid;
                HealtholdBoy hb = new HealtholdBoy();
                sks.Hhs = hb.GetHealtholds(true).Where(h => h.Name.Contains(kword) || h.Notes.Contains(kword) || h.Rawithes.Any(r => r.Notes.Contains(kword)));
            }
            return View(sks);
        }
        [OutputCache(Duration = 1)]
        public ActionResult Majorized(short id, string nm, string nt)
        {
            RecipeBoy boy = new RecipeBoy();
            var mrs = new Models.Recipes
            {
                Mid = id,
                Name = nm,
                Notes = nt,
                Entities = boy.GetMajorecipes(id)
            };
            return View(mrs);
        }
        [OutputCache(Duration = 1)]
        public ActionResult Minorized(short id, string nm, string nt)
        {
            RecipeBoy boy = new RecipeBoy();
            var mrs = new Models.Recipes
            {
                Mid = id,
                Name = nm,
                Notes = nt,
                Entities = boy.GetMinorecipes(id)
            };
            return View(mrs);
        }
    }
}
